// pages/activity/sendBless.js
const app = getApp()
Page({
    data: {
        imgList: [
            "//resource.160nurse.com/canvas/healthy/img/activity/card_1.png",
            "//resource.160nurse.com/canvas/healthy/img/activity/card_2.png"
        ],
        index: 0,
        // 输入框的值
        inputValue: '',
        testLenght: 0
    },
    onLoad: function (options) {
    	this.get_suo = false;
        this.current=0
        console.log(options.goods_id)
        var that = this
        app.request(app.apiDomain + 'index.php?c=mall&a=goodsdetail&goods_id=' + options.goods_id, {},res=>{
            console.log(res);
            var goods = res.data.data
            that.setData({
                goods: goods,
                goods_id: options.goods_id
            })
        })
    },
    selPic() {
        var that = this
        wx.chooseImage({
            count: 1,
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            success(res) {
                // tempFilePath可以作为img标签的src属性显示图片
                that.img_tmp = res.tempFilePaths
                console.log(res.tempFilePaths);
                // that.data.imgList.unshift(res.tempFilePaths[0])
                // that.imgList2=that.data.imgList
                // that.setData({
                //     imgList: that.data.imgList,
                //     index: 1
                // })
                // console.log(that.data.imgList);
                that.img_quality(that.img_tmp[0])
                // 压缩上传
            }
        })
    },
    // 选择上传的那个图片进行压缩
    itemChange(e){
        this.current=e.detail.current
        // e.detail = {current: current, source: source}
    },
    img_quality: function (e) {
        console.log(e);
        var that = this;
        wx.compressImage({
            src: e, // 图片路径
            quality: 60, // 压缩质量
            success: function (res) {
                console.log('fdhg');
                console.log(res)
                that.img_up(res.tempFilePath)
            }
        })
        // }
    },
    img_up: function (a) {
        console.log(11232);
        var that = this;
        wx.uploadFile({ //提交图片
            url: app.apiDomain + "index.php?c=user&a=upload", //接口
            filePath: a,
            name: 'image',
            formData: {
                openid: wx.getStorageSync("openid")
            },
            dataType: 'json',
            method: 'post',
            success: function (res) {
                console.log(res)
                if(res.statusCode==200){
                    // 上传图片成功 
                    var imgSrc=JSON.parse(res.data).data.replace("\\","");
                    console.log(imgSrc);
                    that.data.imgList.unshift("http://resource.160nurse.com"+imgSrc)
                    that.setData({
                        imgList: that.data.imgList,
                        index: 1
                    })
                    // that.setData({
                    //     upLoadSrc: res.data.data
                    // })
                }
            }
        });
    },
    bindKeyInput(e) {
        this.setData({
            inputValue: e.detail.value,
            testLenght: e.detail.value.length
        })
    },
    confirmPay(e) {
    	if(this.get_suo){return false};
    	if(!this.data.inputValue){ 
    		wx.showToast({
                title: '祝福语不能为空',
                icon: 'none',
                duration: 2000
              });
    		return false
    	}
        console.log(this.current);
        console.log(this.data.imgList.length);
        // 判断当前的图片,如果是最后两个,直接上传图片,如果不是,进行压缩
        if(this.current+3>this.data.imgList.length+1){
            console.log(this.data.imgList[this.current-1]);
            this.setData({
                upLoadSrc: this.data.imgList[this.current-1]
            })
        }else{
            // 判断是否为0
            if(this.current==0){
                wx.showToast({
                    title: '请选择封面卡片',
                    icon: 'none',
                    duration: 2000
                  })
                  return false
            }else{
                 // 进行压缩
                // console.log(this.data.imgList[this.current-1])

                // this.img_quality(this.data.imgList[this.current-1])
                this.setData({
                    upLoadSrc: this.data.imgList[this.current-1]
                })
            }
        }
        console.log(this.data.upLoadSrc)
        var time = Date.parse(new Date()).toString();
            time = time.substr(0,10);
        // console.log(app.sign({
        //     f_name: 'ks',
        //     a:2,
        //     c:5
        // }));
        var obj={
            f_channel: 'xiaochengxu',
            time:time,
            benediction: this.data.inputValue,
            goods_id: this.data.goods_id,
            // gift_img: this.data.upLoadSrc,
            num: 1,
            sku: '',
            openid: wx.getStorageSync('openid')
        }
        
        obj.sign = app.sign(obj);
        obj.gift_img = this.data.upLoadSrc;
        console.log(obj);
        //
        this.get_suo = true;
        //
        app.request(app.apiDomain+'index.php?c=mall&a=gifts',obj,res=>{
            console.log(res)
            if(res.data.status==1){
                wx.reLaunch({
                    url: 'sendBless?order_no='+res.data.data
                })
            }else if(res.data.status==2){
                // 微信支付
                wx.requestPayment({
                    'timeStamp': res.data.data.timeStamp,
                    'nonceStr': res.data.data.nonceStr,
                    'package': res.data.data.package,
                    'signType': 'MD5',
                    'paySign': res.data.data.paySign,
                    'success':function(res){},
					'fail':function(res){
							wx.showToast({
								title:'支付失败',
								icon: 'none',
								duration: 2000
							})
					},
                    'complete':function(e){
							console.log(e)
						if(e.errMsg == "requestPayment:ok"){  // 调用支付成功
							wx.redirectTo({
                            	url: '/pages/activity/sendBless?order_no='+res.data.data.order_no
                          	})
						}else if(e.errMsg == 'requestPayment:cancel'){
			　　　　　　		// 用户取消支付的操作
							wx.showToast({
								title:'已取消支付',
								icon: 'none',
								duration: 2000
							})
				　　　 	}
					},
                  })
            }else{
                // 支付失败
                wx.showToast({
                    title: res.data.info,
                    icon: 'none',
                    duration: 2000
                })
            }
        })
    },
	onReady: function() {
		app.SystemInfo(this)
	}
})
